/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.acceptance.worker;

import org.junit.Before;
import org.junit.Test;
import org.ourgrid.acceptance.util.worker.Req_003_Util;
import org.ourgrid.acceptance.util.worker.Req_004_Util;
import org.ourgrid.acceptance.util.worker.Req_087_Util;
import org.ourgrid.acceptance.util.worker.Req_088_Util;
import org.ourgrid.acceptance.util.worker.Req_094_Util;
import org.ourgrid.acceptance.util.worker.Req_120_Util;
import org.ourgrid.common.interfaces.control.WorkerControl;
import org.ourgrid.common.interfaces.to.WorkerStatus;
import org.ourgrid.reqtrace.ReqTest;
import org.ourgrid.worker.WorkerComponent;

import br.edu.ufcg.lsd.commune.container.ObjectDeployment;

@ReqTest(reqs="REQ088")
public class Req_088_Test extends WorkerAcceptanceTestCase {
	
	private Req_003_Util req_003_Util = new Req_003_Util(getComponentContext());
	private Req_004_Util req_004_Util = new Req_004_Util(getComponentContext());
	private Req_120_Util req_120_Util = new Req_120_Util(getComponentContext());
	private Req_094_Util req_094_Util = new Req_094_Util(getComponentContext());
	private Req_087_Util req_087_Util = new Req_087_Util(getComponentContext());
	private Req_088_Util req_088_Util = new Req_088_Util(getComponentContext());
	
	@Before
	public void setUp() throws Exception {
		super.setUp();
		workerAcceptanceUtil.simulateAuthentication();
	}
	
	/**
	 * This test contains the following steps:
	 * 
	 *    1. Start a Worker with the idleness detector enabled and the public
	 *    	 key property set to "publicKey1";
	 *    2. Resume the Worker with the public key "wrongPublicKey";
	 *           * Verify if the following warn message was logged:
	 *           An unknown entity tried to resume the Worker.
	 *           Only the local modules can perform this operation.
	 *           Unknown entity public key: [wrongPublicKey].
	 *    3. Verify if the worker status is OWNER
	 * 
	 */
	@ReqTest(test=" AT-088.1", reqs="REQ088")
	@Test public void test_at_088_1_ResumeWorkerWithWrongPublicKey() throws Exception {
		//Start worker
		WorkerComponent component = req_004_Util.startWorker(true);
		
		//Resume the Worker with wrong public key - expect a warn to be logged
		req_088_Util.resumeWorkerByUnknownEntity(component, "wrongPublicKey");
		
		//Verify if the worker status is OWNER
		req_094_Util.getWorkerStatus(WorkerStatus.OWNER);
	} 
	

	/**
	 * This test contains the following steps:
	 * 
	 *    1. Start a Worker with the idleness detector enabled;
	 *    2. Resume the Worker;
	 *           * Verify if the following info message was logged: "Worker has been RESUMED."
	 *           * Verify if the following debug message was logged: "Status changed from OWNER to IDLE."
	 *           * Verify if the ControlClient received the operation succeed message
	 *    3. Verify if the worker status is IDLE
	 * 
	 */
	@ReqTest(test=" AT-088.2", reqs="REQ088")
	@Test public void test_at_088_2_ResumeBusyWorker() throws Exception {
		//Start worker
		WorkerComponent component = req_004_Util.startWorker(true);
		
		//Resume the Worker
		req_088_Util.resumeOwnerWorker(component);
		
		//Verify if the worker status is IDLE
		req_094_Util.getWorkerStatus(WorkerStatus.IDLE);
	}
	
	/**
	 * This test contains the following steps:
	 * 
	 *    1. Start a Worker with the idleness detector disabled;
	 *    2. Pause the Worker;
	 *           * Verify if the following info message was logged: "Worker has been PAUSED."
	 *           * Verify if the following debug message was logged: "Status changed from IDLE to OWNER."
	 *           * Verify if the ControlClient received the operation succeed message
	 *    3. Resume the Worker;
	 *           * Verify if the following info message was logged: "Worker has been RESUMED."
	 *           * Verify if the following debug message was logged: "Status changed from OWNER to IDLE."
	 *           * Verify if the ControlClient received the operation succeed message
	 *    4. Verify if the worker status is IDLE
	 * 
	 */
	@ReqTest(test=" AT-088.3", reqs="REQ088")
	@Test public void test_at_088_3_ResumePausedWorker() throws Exception {
		//Start worker
		WorkerComponent component = req_004_Util.startWorker();
		
		//Pause the Worker
		req_087_Util.pauseIdleWorker(component);
		
		//Resume the Worker
		req_088_Util.resumeOwnerWorker(component);
		
		//Verify if the worker status is IDLE
		req_094_Util.getWorkerStatus(WorkerStatus.IDLE);
	}
	
	/**
	 * This test contains the following steps:
	 * 
	 *    1. Start a Worker with the idleness detector disabled;
	 *    2. Resume the Worker;
	 *           * Verify if the ControlClient received the operation succeed message
	 *    3. Verify if the worker status is IDLE
	 *    
	 */
	@ReqTest(test=" AT-088.4", reqs="REQ088")
	@Test public void test_at_088_4_ResumeIdleWorker() throws Exception {
		//Start worker
		WorkerComponent component = req_004_Util.startWorker();
		
		//Resume the Worker
		req_088_Util.resumeNotOwnerWorker(component);
		
		//Verify if the worker status is IDLE
		req_094_Util.getWorkerStatus(WorkerStatus.IDLE);
	}
	
	/**
	 * This test contains the following steps:
	 * 
	 *    1. Create the Worker (do not start it)
	 *    2. Pause the Worker
	 *           * Verify if the ControlClient received the operation succeed message with the ComponentNotStarted error
	 *    3. Resume the Worker
	 *           * Verify if the ControlClient received the operation succeed message with the ComponentNotStarted error
	 *    4. Stop the Worker
	 *           * Verify if the ControlClient received the operation succeed message with the ComponentNotStarted error
	 *    5. Start the Worker
	 *    6. Start the Worker
	 *           * Verify if the ControlClient received the operation succeed message with the ComponentAlreadyStarted error
	 *    7. Stop the Worker
	 *    8. Stop the Worker
	 *           * Verify if the ControlClient received the operation succeed message with the ComponentStoppedException error
	 *    9. Resume the Worker
	 *           * Verify if the ControlClient received the operation succeed message with the ComponentStoppedException error
	 *   10. Pause the Worker
	 *           * Verify if the ControlClient received the operation succeed message with the ComponentStoppedException error
	 * 
	 */
	@ReqTest(test=" AT-088.5", reqs="REQ088")
	@Test public void test_at_088_5_WorkerStatesMachine() throws Exception {
		//Create the worker
		WorkerComponent component = req_003_Util.createWorkerComponent();

		//Pause the Worker
		req_087_Util.pauseUnstartedWorker(component);
		
		//Resume the Worker
		req_088_Util.resumeUnstartedWorker(component);
		
		//Stop worker
		req_120_Util.stopUnstartedWorker(component);
		
		//Start the worker
		req_004_Util.startWorker(component, null, false);
		
		//Start the worker
		component = req_004_Util.startWorker(component, null, true);
		
		//Stop worker
		WorkerControl workerControl = workerAcceptanceUtil.getWorkerControl();
		ObjectDeployment wcOD = workerAcceptanceUtil.getWorkerControlDeployment();
		req_120_Util.stopWorker(component);
		
		//Stop worker
		req_120_Util.stopStoppedWorker(component, workerControl, wcOD.getDeploymentID().getPublicKey());
		
		//Resume the Worker
		req_088_Util.resumeStoppedWorker(component, workerControl);
		
		//Pause the Worker
		req_087_Util.pauseStoppedWorker(component, workerControl);
	}
}